package com.transfiguro;

import com.transfiguro.core.Core;
import com.transfiguro.datamodel.csmodels.CSClassModel;
import jsyntaxpane.DefaultSyntaxKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDropEvent;
import java.io.*;
import java.util.List;

public class JavaFileView implements Runnable {
    private final Logger logger = LoggerFactory.getLogger(JavaFileView.class);
    public JEditorPane getEditorPane() {
        return editorPane;
    }

    private JEditorPane editorPane;
    private JScrollPane scrollPane;
    private JEditorPane csEditorPane;
    private JScrollPane csScrollPane;
    private CSFileView csFileView;
    private Core core;

    public JavaFileView() {
        editorPane = new JEditorPane();
        csEditorPane = new JEditorPane();
        scrollPane = new JScrollPane(editorPane);
        csScrollPane = new JScrollPane(csEditorPane);
        csFileView = new CSFileView();
        core = new Core();
        DefaultSyntaxKit.initKit();
        //scrollPane.setSize(500, 500);
        Thread thread = new Thread(this);
        thread.start();
    }

    public JScrollPane getScrollPane() {
        return scrollPane;
    }

    @Override
    public void run() {
        editorPane.setDragEnabled(true);
        csEditorPane.setContentType("text/java");
        csEditorPane.setToolTipText("CONVERTED FILE WILL APPEAR HERE");
        editorPane.setEditable(false);
        editorPane.setToolTipText("DROP YOUR JAVA FILE HERE");
        editorPane.setContentType("text/java");
        editorPane.setDropTarget(new DropTarget() {
            public synchronized void drop(DropTargetDropEvent evt) {
                try {
                    evt.acceptDrop(DnDConstants.ACTION_COPY);
                    List<File> droppedFiles = (List<File>)
                            evt.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
                    StringBuilder fileString = new StringBuilder();
                    for (File file : droppedFiles) {
                        editorPane.setText("");
                        final BufferedReader reader = new BufferedReader(new FileReader(file));
                        char[] buf = new char[1024];
                        int charsRead = 0;
                        while((charsRead = reader.read(buf))!=-1) {
                            fileString.append(String.valueOf(buf, 0, charsRead));
                            buf = new char[1024];
                        }
                        editorPane.setText(fileString.toString());
                        final CSClassModel csClassModel = core.getConvertedFile(file);
                        logger.info("CSClass: \n{}", csClassModel.toString());
                        csEditorPane.setText(csClassModel.toString());
                    }
                } catch (final FileNotFoundException e) {
                    logger.debug("Exception: {}", e.getMessage());
                } catch (final UnsupportedFlavorException e) {
                    logger.debug("Exception: {}", e.getMessage());
                } catch (final IOException e) {
                    logger.debug("Exception: {}", e.getMessage());
                }
            }
        });
    }

    public CSFileView getCsFileView() {
        return csFileView;
    }

    public JEditorPane getCsEditorPane() {
        return csEditorPane;
    }

    public void setCsEditorPane(final JEditorPane csEditorPane) {
        this.csEditorPane = csEditorPane;
    }

    public JScrollPane getCsScrollPane() {
        return csScrollPane;
    }

    public void setCsScrollPane(final JScrollPane csScrollPane) {
        this.csScrollPane = csScrollPane;
    }
}
